package mysql

import (
	"errors"
	"example.com/m/v2/internal/infrastructure/config"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

func InitGorm(config *config.CliConfig) (*gorm.DB, error) {
	log.Printf("gorm client constructed")
	if config == nil || config.DbConfig == nil {
		return nil, errors.New("config error, init gorm")
	}
	db, err := gorm.Open(mysql.Open(config.DbConfig.Dsn), &gorm.Config{
		PrepareStmt: config.DbConfig.PrepareStmt,
	})
	if err != nil {
		return nil, err
	}
	return db, nil
}
